package com.hengtongxing.hejiayun.widget;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hengtongxing.hejiayun.R;


/**
 *
 */

public class AppTitleBar extends RelativeLayout implements View.OnClickListener {

    private int backgroundId;
    private int titleColor;
    private float titleSize;
    private LinearLayout ll_left;
    private TextView tv_title;
    private TextView tv_rightText;
    private RightClickListener clickListenerRight;
    private LeftClickListener clickListenerLeft;
    private Activity activity;
    private boolean showLeftIcon;
    private boolean showTitle;
    private boolean showRightIcon;
    private boolean showRightText;
    private String title = "";
    private int titleStyle;
    private String rightTip = "";
    private RelativeLayout rl_rootView;
    private View view_leftIcon;
    private int leftIconId;
    private int rightIconId;
    private View view_rightIcon;
    private View ll_right;
    private int rightTipColor;
    private float rightTipSize;
    private int rightTipStyle;

    public AppTitleBar(Context context) {
        this(context, null);
    }

    public AppTitleBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AppTitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        getAttrs(context, attrs);
        initView(context);
    }

    public void setTitle(String title) {
        this.title = title;
        setTitle();
    }

    public void setTitleStyle(int titleStyle) {
        this.titleStyle = titleStyle;
        setTitle();
    }

    public void setTitleColor(int titleColor) {
        this.titleColor = titleColor;
        setTitle();
    }

    public void setTitleSize(float titleSize) {
        this.titleSize = titleSize;
    }

    public void setTitleRightColor(int rightColor) {
        tv_rightText.setTextColor(rightColor);
    }

    public void setTitleRightText(String rightText) {
        tv_rightText.setText(rightText);
    }

    public void setRightClickListener(RightClickListener clickListenerRight) {
        this.clickListenerRight = clickListenerRight;
    }

    public void setLeftClickListener(LeftClickListener clickListenerLeft) {

        this.clickListenerLeft = clickListenerLeft;
    }


    private void getAttrs(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AppTitleBar);
        backgroundId = typedArray.getResourceId(R.styleable.AppTitleBar_background, R.color.windowBackg);


        leftIconId = typedArray.getResourceId(R.styleable.AppTitleBar_leftIcon, R.mipmap.ic_back_white);

        title = typedArray.getString(R.styleable.AppTitleBar_title);
        titleColor = typedArray.getColor(R.styleable.AppTitleBar_title_color, getResources().getColor(R.color.white));
        titleSize = typedArray.getDimension(R.styleable.AppTitleBar_title_size, getResources().getDimension(R.dimen.title_bar));
        titleStyle = typedArray.getInt(R.styleable.AppTitleBar_title_style, 0);

        rightTip = typedArray.getString(R.styleable.AppTitleBar_rightTip);
        rightTipColor = typedArray.getColor(R.styleable.AppTitleBar_rightTip_color, getResources().getColor(R.color.white));
        rightTipSize = typedArray.getDimension(R.styleable.AppTitleBar_rightTip_size, getResources().getDimension(R.dimen.dp_14));
        rightTipStyle = typedArray.getInt(R.styleable.AppTitleBar_rightTip_style, 0);
        rightIconId = typedArray.getResourceId(R.styleable.AppTitleBar_rightIcon, R.mipmap.ic_back_white);

        showLeftIcon = typedArray.getBoolean(R.styleable.AppTitleBar_show_leftIcon, true);
        showTitle = typedArray.getBoolean(R.styleable.AppTitleBar_show_title, true);
        showRightIcon = typedArray.getBoolean(R.styleable.AppTitleBar_show_rightIcon, false);
        showRightText = typedArray.getBoolean(R.styleable.AppTitleBar_show_rightText, false);

        typedArray.recycle();
    }

    private void initView(Context context) {

        activity = (Activity) context;// 获取当前TitleBar所在的Activity

        View view = View.inflate(context, R.layout.app_title_bar, AppTitleBar.this);

        rl_rootView = view.findViewById(R.id.rl_rootView);
        ll_left = view.findViewById(R.id.ll_left);
        ll_right = view.findViewById(R.id.ll_right);
        view_leftIcon = view.findViewById(R.id.view_leftIcon);
        view_rightIcon = view.findViewById(R.id.view_rightIcon);
        tv_title = view.findViewById(R.id.tv_AppTitle);
        tv_rightText = view.findViewById(R.id.tv_rightText);


        rl_rootView.setBackgroundResource(backgroundId);
        view_leftIcon.setBackgroundResource(leftIconId);
        view_rightIcon.setBackgroundResource(rightIconId);

        setViewVisibilityState();

        ll_left.setOnClickListener(this);
        ll_right.setOnClickListener(this);
    }

    public void setLeftIcon(int leftIconId) {
        view_leftIcon.setBackgroundResource(leftIconId);
    }

    private void setTitle() {
        tv_title.setText(title);
        tv_title.setTextColor(titleColor);
        tv_title.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
        if (titleStyle == 0) {
            tv_title.setTypeface(Typeface.DEFAULT, Typeface.NORMAL);
        } else if (titleStyle == 1) {
            tv_title.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
        } else if (titleStyle == 2) {
            tv_title.setTypeface(Typeface.DEFAULT, Typeface.ITALIC);
        } else if (titleStyle == 3) {
            tv_title.setTypeface(Typeface.DEFAULT, Typeface.BOLD_ITALIC);
        }
    }

    private void setRightTxt() {
        tv_rightText.setText(rightTip);
        tv_rightText.setTextColor(rightTipColor);
        tv_rightText.setTextSize(TypedValue.COMPLEX_UNIT_PX, rightTipSize);
        if (rightTipStyle == 0) {
            tv_rightText.setTypeface(Typeface.DEFAULT, Typeface.NORMAL);
        } else if (rightTipStyle == 1) {
            tv_rightText.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
        } else if (rightTipStyle == 2) {
            tv_rightText.setTypeface(Typeface.DEFAULT, Typeface.ITALIC);
        } else if (rightTipStyle == 3) {
            tv_rightText.setTypeface(Typeface.DEFAULT, Typeface.BOLD_ITALIC);
        }
    }

    public void setRightTxt(String rightTip) {
        tv_rightText.setText(rightTip);
    }

    private void setViewVisibilityState() {

        if (showTitle) {
            tv_title.setVisibility(VISIBLE);
            setTitle();
        } else {
            tv_title.setVisibility(GONE);
        }

        if (showLeftIcon) {
            ll_left.setVisibility(VISIBLE);
        } else {
            ll_left.setVisibility(GONE);
        }

        if (showRightText) {
            setRightTxt();
            tv_rightText.setVisibility(VISIBLE);
        } else {
            tv_rightText.setVisibility(GONE);
        }

        if (showRightIcon) {
            view_rightIcon.setVisibility(VISIBLE);
        } else {
            view_rightIcon.setVisibility(GONE);
        }


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_left:
                if (clickListenerLeft != null) {
                    clickListenerLeft.onClickLeft();
                } else {
                    activity.finish();
                }

                break;
            case R.id.ll_right:
                if (clickListenerRight != null) {
                    clickListenerRight.onClickRight();
                }
                break;
        }
    }


    public interface RightClickListener {
        void onClickRight();
    }

    public interface LeftClickListener {
        void onClickLeft();
    }
}
